package opensearch_industry

import (
	"fmt"
	conf "gitee.com/wuzheng0709/backend-gopkg/infrastructure/config"
	"gitee.com/wuzheng0709/backend-gopkg/infrastructure/pkg/gin/log"
	opensearch "gitee.com/wuzheng0709/backend-gopkg/infrastructure/pkg/opensearch_industry/client"
	util "github.com/alibabacloud-go/tea-utils/service"
	"github.com/alibabacloud-go/tea/tea"
	"strconv"
)

var config = &opensearch.Config{
	Endpoint:        tea.String(conf.C.OpenSearchIndustry.Endpoint),
	AccessKeyId:     tea.String(conf.C.OpenSearchIndustry.AccessKeyId),
	AccessKeySecret: tea.String(conf.C.OpenSearchIndustry.AccessKeySecret),
}

// 请求发送的配置参数. 用以请求配置及连接池配置.
var runTime = &util.RuntimeOptions{
	ConnectTimeout: tea.Int(5000),
	ReadTimeout:    tea.Int(10000),
	Autoretry:      tea.Bool(false),
	IgnoreSSL:      tea.Bool(false),
	MaxIdleConns:   tea.Int(50),
}

// appName 是 app 名称.
var Knowledge string = "knowledge"
var KnowledgePart string = "knowledge_part"

var tableName = "knowledge"

// modelName 是 下拉提示产出模型的模型名.
//var modelName = "pull_name"

//func PullTip(words string) {
//	// New  一个client, 用以发送请求.
//	client, _clientErr := opensearch.NewClient(config)
//
//	// 如果 NewClient 过程中出现异常. 则 返回 _client_err 且输出 错误信息.
//	if _clientErr != nil {
//		fmt.Println(_clientErr)
//		return
//	}
//
//	requestParams := map[string]interface{}{
//		"hit":   10,
//		"query": words,
//	}
//
//	// 发送请求的方法调用.
//	response, _requestErr := client.Request(
//		tea.String("GET"),
//		tea.String("/v3/openapi/apps/"+appName+"/suggest/"+modelName+"/search"),
//		requestParams,
//		nil,
//		nil,
//		runTime)
//	// 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
//	if _requestErr != nil {
//		fmt.Println(_requestErr)
//		return
//	}
//
//	// 输出正常返回的 response 内容.
//	fmt.Println(response)
//}

type requestParm struct {
	method   *string
	pathname *string
	query    map[string]interface{}
	headers  map[string]*string
	body     interface{}
}

func NewRequest(option string, query map[string]interface{}, headers map[string]*string, body interface{}, appName, modelName string) *requestParm {
	rp := new(requestParm)
	switch option {
	case "searchAll":
		rp.method = tea.String("GET")
		rp.pathname = tea.String("/v3/openapi/apps/" + appName + "/search")
		rp.query = query
		rp.headers = headers
		rp.body = body
		return rp
	case "pullTip":
		rp.method = tea.String("GET")
		rp.pathname = tea.String("/v3/openapi/apps/" + appName + "/suggest/" + modelName + "/search")
		rp.query = query
		rp.headers = headers
		rp.body = body
		return rp
	case "hotSearch":
		rp.method = tea.String("GET")
		rp.pathname = tea.String("/v3/openapi/apps/" + appName + "/actions/hot")
		rp.query = query
		rp.headers = headers
		rp.body = body
		return rp
	case "optionData":
		rp.method = tea.String("POST")
		rp.pathname = tea.String("/v3/openapi/apps/" + appName + "/" + tableName + "/actions/bulk")
		rp.query = nil
		rp.headers = nil
		rp.body = body
		return rp
	}
	return nil
}

// 请求阿里云
func (rn *requestParm) RequestAliyunApi() (map[string]interface{}, error) {
	// New  一个client, 用以发送请求.
	client, _clientErr := opensearch.NewClient(config)

	// 如果 NewClient 过程中出现异常. 则 返回 _clientErr 且输出 错误信息.
	if _clientErr != nil {
		log.Errorf("NewClient 过程中出现异常. 且输出 错误信息 %s:", _clientErr)
		return nil, _clientErr
	}

	// 发送请求的方法调用.
	response, _requestErr := client.Request(
		rn.method,
		rn.pathname,
		rn.query,
		rn.headers,
		rn.body,
		runTime)

	// 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
	if _requestErr != nil {
		log.Errorf("发送请求 过程中出现异常. 且输出 错误信息: %s", _requestErr)
		return nil, _requestErr
	}
	log.Infof("记录查询阿里数据信息", response)
	return response, nil
}

// opensearch 支持 'ADD' / 'UPDATE' 及 'DELETE' 命令.分别实现 新增,更新 和删除 doc 动作
// option: ADD UPDATE DELETE
func OptionData(option string, uuid int, name, url, mainTag, assistTag, tags, appName string) {
	client, _clientErr := opensearch.NewClient(config)
	if _clientErr != nil {
		fmt.Println(_clientErr)
		return
	}

	document1st := map[string]interface{}{
		"cmd": option,
		"fields": map[string]interface{}{
			"uuid":       uuid,
			"desc":       strconv.Itoa(uuid), // doc 字段.
			"name":       name,
			"url":        url,
			"main_tag":   mainTag,
			"assist_tag": assistTag,
			"tags":       tags,
		},
	}

	// 组装推送文档 数组结构. 其中包含单条文档动作.
	requestBody := []interface{}{document1st}

	// 发送请求的方法调用.
	response, _requestErr := client.Request(
		tea.String("POST"),
		tea.String("/v3/openapi/apps/"+appName+"/"+tableName+"/actions/bulk"),
		nil,
		nil,
		requestBody,
		runTime)

	// 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
	if _requestErr != nil {
		fmt.Println("wwwww", _requestErr)
		return
	}

	// 输出正常返回的 response 内容.
	fmt.Println(response)
}
